package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.ChannelRoleService;
import com.yixun.qfbao.apidto.AddChannelRoleReqDto;
import com.yixun.qfbao.apidto.AddChannelRoleResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AddChannelRoleReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新增角色
 * @author guifang.weng
 * @create 2017-11-22 15:19
 **/
@Service
public class AddChannelRoleHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AddChannelRoleHander.class);

    @Autowired
    private ChannelRoleService channelRoleService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<AddChannelRoleResDto> res = new BaseResponseDTO<>();
        AddChannelRoleReqVo reqVo = (AddChannelRoleReqVo) reqDto;
        AddChannelRoleReqDto addChannelRoleReqDto = reqVo.getParams();
        PartnerInfo pi = loginUtils.getUser(addChannelRoleReqDto.getUserId());
        if(pi == null){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        addChannelRoleReqDto.setMerchantId(pi.getHigherid());
        try {
            AddChannelRoleResDto resDto = channelRoleService.addChannelRole(addChannelRoleReqDto);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("AddChannelRoleHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("新增角色失败");
        }
        return res;
    }
}
